package io.geekidea.springbootplus.system.controller;


import io.geekidea.springbootplus.framework.common.api.ApiResult;
import io.geekidea.springbootplus.framework.common.controller.BaseController;
import io.geekidea.springbootplus.framework.core.pagination.Paging;
import io.geekidea.springbootplus.framework.log.annotation.Module;
import io.geekidea.springbootplus.framework.log.annotation.OperationLog;
import io.geekidea.springbootplus.framework.log.enums.OperationLogType;
import io.geekidea.springbootplus.system.entity.SysCategory;
import io.geekidea.springbootplus.system.param.SysCategoryPageParam;
import io.geekidea.springbootplus.system.service.SysCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/sysCategory")
@Module("system")
@Api(value = "商品类别API", tags = {"商品类别"})
public class SysCategoryController extends BaseController {


    @Autowired
    private SysCategoryService sysCategoryService;


    @PostMapping("/add")
    @OperationLog(name = "添加商品类别", type = OperationLogType.ADD)
    @ApiOperation(value = "添加商品类别", response = ApiResult.class)
    public ApiResult<Boolean> addSysCategory(@Validated @RequestBody SysCategory sysCategory) throws Exception {
        boolean flag = sysCategoryService.save(sysCategory);
        return ApiResult.result(flag);
    }


    @PostMapping("/update")
    @OperationLog(name = "修改商品类别", type = OperationLogType.UPDATE)
    @ApiOperation(value = "修改商品类别", response = ApiResult.class)
    public ApiResult<Boolean> updateSysCategory(@Validated @RequestBody SysCategory sysCategory) throws Exception {
        boolean flag = sysCategoryService.updateCategory(sysCategory);
        return ApiResult.result(flag);
    }

    @PostMapping("/delete/{id}")
    @OperationLog(name = "删除商品类别", type = OperationLogType.DELETE)
    @ApiOperation(value = "删除商品类别", response = ApiResult.class)
    public ApiResult<Boolean> deleteSysCategory(@PathVariable("id") Integer id) throws Exception {
        boolean flag = sysCategoryService.deleteCategoryById(id);
        return ApiResult.result(flag);
    }

    @GetMapping("/info/{id}")
    @OperationLog(name = "商品类别详情", type = OperationLogType.INFO)
    @ApiOperation(value = "商品类别详情")
    public ApiResult<SysCategory> getSysCategory(@PathVariable("id") Integer id) throws Exception {
        SysCategory sysCategory = sysCategoryService.getCategoryById(id);
        return ApiResult.ok(sysCategory);

    }


    @PostMapping("/plist/param")
    @OperationLog(name = "商品类别分页列表", type = OperationLogType.PAGE)
    @ApiOperation(value = "商品类别分页列表", response = SysCategory.class)
    public ApiResult<Paging<SysCategory>> getSysCategoryPageListsByParam(@RequestBody SysCategoryPageParam sysCategoryPageParam) throws Exception {
        Paging<SysCategory> paging = sysCategoryService.getSysCategoryPageListByParam(sysCategoryPageParam);
        return ApiResult.ok(paging);
    }

}
